﻿using Oracle.ManagedDataAccess.Client;
using ORAProvider;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UserService.Entity;

namespace UserService.Repository.Repositories
{
    public class ClientRepository
    {
        private readonly OracleDataContext _db;
        public ClientRepository(OracleDataContext oracleContext)
        {
            _db = oracleContext;
        }

        public Client GetClientById(string clientId)
        {
            var result = _db.ExecuteQuery(
              "SELECT * FROM CLIENTS WHERE CLIENT_ID = :CLIENT_ID",
              new OracleParameter { ParameterName = ":CLIENT_ID", Value = clientId, OracleDbType = OracleDbType.Varchar2, Direction = ParameterDirection.Input });

            var row = result.Rows.Cast<DataRow>().SingleOrDefault();

            if (row != null)
            {
                return new Client
                {
                    id = row["id"].ToString(),
                    client_id = row["client_id"].ToString(),
                    client_url = row["client_url"].ToString(),
                    client_secret = row["client_secret"].ToString(),
                    client_scope = row["client_scope"].ToString(),
                    client_code = row["client_code"].ToString()
                };
            }

            return null;
        }

        public int AddClient(Client obj)
        {
            return _db.ExecuteNonQuery(
                "INSERT INTO CLIENTS (id,client_id,client_url,client_secret,client_scope,client_code ) VALUES (:id,:client_id,:client_url,:client_secret,:client_scope,:client_code )",
                new OracleParameter { ParameterName = ":id", Value = obj.id, OracleDbType = OracleDbType.Varchar2, Direction = ParameterDirection.Input },
                new OracleParameter { ParameterName = ":client_id", Value = obj.client_id, OracleDbType = OracleDbType.Varchar2, Direction = ParameterDirection.Input },
                new OracleParameter { ParameterName = ":client_url", Value = obj.client_url, OracleDbType = OracleDbType.Varchar2, Direction = ParameterDirection.Input },
                new OracleParameter { ParameterName = ":client_secret", Value = obj.client_secret, OracleDbType = OracleDbType.Varchar2, Direction = ParameterDirection.Input },
                new OracleParameter { ParameterName = ":client_scope", Value = obj.client_scope, OracleDbType = OracleDbType.Varchar2, Direction = ParameterDirection.Input },
                new OracleParameter { ParameterName = ":client_code", Value = obj.client_code, OracleDbType = OracleDbType.Varchar2, Direction = ParameterDirection.Input });
            //return false;
        }
    }
}
